package com.aivin.lib_widget.bean;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.aivin.lib_base.tool.WkLog;
import com.aivin.lib_widget.R;

/**
 * 瀑布流的间隔 ItemDecoration
 */
public class MarginDecorationForStaggered extends RecyclerView.ItemDecoration {
    private int margin;
    private int columnCount;

    public MarginDecorationForStaggered(Context context) {
        margin = context.getResources().getDimensionPixelSize(R.dimen.aivin_item_margin);
    }

    public MarginDecorationForStaggered(Context context , int columnCount ,int margin) {
        this.margin=margin ;
        this.columnCount=columnCount ;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        int left;
        int right= margin;
        int top;
        int bottom =0;

        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        int position = params.getSpanIndex();
        if (position == RecyclerView.NO_POSITION) {
            return;
        }

        //WkLog.showLog("position=="+position);
        // top
        if(position < columnCount ){
            top = margin ;
        }else{
            top = 0 ;
        }

        // left
        if(position%columnCount == 0){
            left = margin ;
        }else{
            left = 0 ;
        }
        outRect.set(left, top, right, bottom);
    }
}
